﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LP580.CG
{
    /// <summary>
    /// clsGiftProduct 的摘要说明
    /// <summary>
    public class clsGiftProduct
    {

        /// <summary>
        /// 文件存放文件夹（前后无“/”）
        /// 实际存入数据库时上一层还有根路径如：/upfiles/
        /// </summary>
        public readonly string _FileSaveDir = "GiftProductsPic";

        #region 构造函数

        public clsGiftProduct() { }

        public clsGiftProduct(int prmGiftProductID)
        {
            clsGiftProduct entity = clsGiftProduct.GetByKey(prmGiftProductID);
            this._giftproductid = entity._giftproductid;
            this._userid = entity._userid;
            this._giftproductname = entity._giftproductname;
            this._giftproductclassid = entity._giftproductclassid;
            this._giftproducttypeid = entity._giftproducttypeid;
            this._giftproductinfo = entity._giftproductinfo;
            this._giftproductpricespanid = entity._giftproductpricespanid;
            this._giftproductunit = entity._giftproductunit;
            this._giftproductminorder = entity._giftproductminorder;
            this._giftproductoutertime = entity._giftproductoutertime;
            this._giftproductaddtime = entity._giftproductaddtime;
            this._giftproductmaterial = entity._giftproductmaterial;
            this._giftproductpacking = entity._giftproductpacking;
            this._giftproductsize = entity._giftproductsize;
            this._packingnum = entity._packingnum;
            this._giftproductfavorites = entity._giftproductfavorites;
            this._giftproducthits = entity._giftproducthits;
            this._giftproductpics = entity._giftproductpics;
        }

        #endregion

        public static clsGiftProduct GetByKey(int prmGiftProductID)
        {
            clsGiftProduct entity = new clsGiftProduct();
            DBase dbo = new DBase();
            System.Data.IDataReader dr = dbo.ExecuteReader("SELECT * FROM [tbl_GiftProduct] WHERE GiftProductID=" + prmGiftProductID.ToString());
            if (dr.Read())
            {
                entity = DrToClass(dr);
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            return entity;
        }

        private static clsGiftProduct DrToClass(System.Data.IDataReader dr)
        {
            clsGiftProduct entity = new clsGiftProduct();

            int iTmp = 0;
            if (!int.TryParse(dr["GiftProductID"].ToString(), out iTmp))
                iTmp = 0;
            entity._giftproductid = iTmp;
            iTmp = 0;
            if (!int.TryParse(dr["UserID"].ToString(), out iTmp))
                iTmp = 0;
            entity._userid = iTmp;
            entity._giftproductname = dr["GiftProductName"].ToString();
            iTmp = 0;
            if (!int.TryParse(dr["GiftProductClassID"].ToString(), out iTmp))
                iTmp = 0;
            entity._giftproductclassid = iTmp;
            iTmp = 0;
            if (!int.TryParse(dr["GiftProductTypeID"].ToString(), out iTmp))
                iTmp = 0;
            entity._giftproducttypeid = iTmp;
            entity._giftproductinfo = dr["GiftProductInfo"].ToString();
            iTmp = 0;
            if (!int.TryParse(dr["GiftProductPriceSpanID"].ToString(), out iTmp))
                iTmp = 0;
            entity._giftproductpricespanid = iTmp;
            entity._giftproductunit = dr["GiftProductUnit"].ToString();
            iTmp = 0;
            if (!int.TryParse(dr["GiftProductMinOrder"].ToString(), out iTmp))
                iTmp = 0;
            entity._giftproductminorder = iTmp;
            DateTime dtTmp = new DateTime(1900, 1, 1);
            if (!int.TryParse(dr["GiftProductOuterTime"].ToString(), out iTmp))
                dtTmp = new DateTime(1900, 1, 1);
            entity._giftproductoutertime = iTmp;
            dtTmp = new DateTime(1900, 1, 1);
            if (!DateTime.TryParse(dr["GiftProductAddTime"].ToString(), out dtTmp))
                dtTmp = new DateTime(1900, 1, 1);
            entity._giftproductaddtime = dtTmp;
            iTmp = 0;
            if (!int.TryParse(dr["GiftProductHits"].ToString(), out iTmp))
                iTmp = 0;
            entity._giftproducthits = iTmp;

            entity._giftproductpics = new List<string>();
            for (int i = 1; i <= 5; i++)
            {
                entity._giftproductpics.Add(dr["GiftProductPic" + i].ToString());
            }


            entity._giftproductmaterial = dr["GiftProductMaterial"].ToString();
            entity._giftproductpacking = dr["GiftProductPacking"].ToString();
            entity._giftproductsize = dr["GiftProductSize"].ToString();
            int intTmp = 0;
            if (!int.TryParse(dr["PackingNum"].ToString(), out intTmp))
                intTmp = 0;
            entity._packingnum = intTmp;
            if (!int.TryParse(dr["GiftProductFavorites"].ToString(), out intTmp))
                intTmp = 0;
            entity._giftproductfavorites = intTmp;

            return entity;
        }

        /// <summary>
        ///  处理被收藏统计数量变化
        /// </summary>
        public static void DealFavoriteCount(int giftid, int s)
        {
            string sql = string.Format("UPDATE [tbl_GiftProduct] SET GiftProductFavorites=GiftProductFavorites+{0} WHERE (GiftProductID = {1})", s, giftid);
            DBase dbo = new DBase();
            dbo.ExecSql(sql);
            dbo.CloseConn();
            dbo = null;
        }

        /// <summary>
        /// 被收藏一次
        /// </summary>
        public static void AddFavorite(int giftid)
        {
            DealFavoriteCount(giftid, 1);
        }

        /// <summary>
        /// 被取消收藏
        /// </summary>
        public static void DecFavorite(int giftid)
        {
            DealFavoriteCount(giftid, -1);
        }

        #region 增删改数据库操作

        public static int Insert(clsGiftProduct prmEntity)
        {
            DataTableManage SaveDTM = new DataTableManage();
            string sql;
            sql = string.Format("insert into [tbl_GiftProduct](" +
                            " [UserID]," +
                            " [GiftProductName]," +
                            " [GiftProductClassID]," +
                            " [GiftProductTypeID]," +
                            " [GiftProductInfo]," +
                            " [GiftProductPriceSpanID]," +
                            " [GiftProductUnit]," +
                            " [GiftProductMinOrder]," +
                            " [GiftProductOuterTime]," +
                            " [GiftProductAddTime]," +
                                " [GiftProductMaterial]," +
                                " [GiftProductPacking]," +
                                " [GiftProductSize]," +
                                " [PackingNum]," +
                                " [GiftProductFavorites]," +
                            " [GiftProductHits]," +
                            " [GiftProductPic1]," +
                            " [GiftProductPic2]," +
                            " [GiftProductPic3]," +
                            " [GiftProductPic4]," +
                            " [GiftProductPic5]" +
            ") values (" +
                    "@UserID," +
                    "@GiftProductName," +
                    "@GiftProductClassID," +
                    "@GiftProductTypeID," +
                    "@GiftProductInfo," +
                    "@GiftProductPriceSpanID," +
                    "@GiftProductUnit," +
                    "@GiftProductMinOrder," +
                    "@GiftProductOuterTime," +
                    "@GiftProductAddTime," +
                        "@GiftProductMaterial," +
                        "@GiftProductPacking," +
                        "@GiftProductSize," +
                        "@PackingNum," +
                        "@GiftProductFavorites," +
                    "@GiftProductHits," +
                    "@GiftProductPic1," +
                    "@GiftProductPic2," +
                    "@GiftProductPic3," +
                    "@GiftProductPic4," +
                    "@GiftProductPic5" +
            ")");
            SaveDTM.Sql = sql;
            SaveDTM.AddNewParam("@UserID", prmEntity._userid);
            SaveDTM.AddNewParam("@GiftProductName", prmEntity._giftproductname);
            SaveDTM.AddNewParam("@GiftProductClassID", prmEntity._giftproductclassid);
            SaveDTM.AddNewParam("@GiftProductTypeID", prmEntity._giftproducttypeid);
            SaveDTM.AddNewParam("@GiftProductInfo", prmEntity._giftproductinfo);
            SaveDTM.AddNewParam("@GiftProductPriceSpanID", prmEntity._giftproductpricespanid);
            SaveDTM.AddNewParam("@GiftProductUnit", prmEntity._giftproductunit);
            SaveDTM.AddNewParam("@GiftProductMinOrder", prmEntity._giftproductminorder);
            SaveDTM.AddNewParam("@GiftProductOuterTime", prmEntity._giftproductoutertime);
            SaveDTM.AddNewParam("@GiftProductAddTime", prmEntity._giftproductaddtime);
            SaveDTM.AddNewParam("@GiftProductMaterial", prmEntity._giftproductmaterial);
            SaveDTM.AddNewParam("@GiftProductPacking", prmEntity._giftproductpacking);
            SaveDTM.AddNewParam("@GiftProductSize", prmEntity._giftproductsize);
            SaveDTM.AddNewParam("@PackingNum", prmEntity._packingnum);
            SaveDTM.AddNewParam("@GiftProductFavorites", prmEntity._giftproductfavorites);
            SaveDTM.AddNewParam("@GiftProductHits", prmEntity._giftproducthits);
            for (int i = 1; i <= 5; i++)
            {
                SaveDTM.AddNewParam("@GiftProductPic" + i, prmEntity._giftproductpics[i - 1]);
            }
            SaveDTM.Insert();

            //对应产品分类统计数增一
            new clsGiftProductClass(prmEntity._giftproductclassid).AddGiftProduct();

            //对应用户所有产品数增一
            clsUserCompanyInfo.AddGiftProduct(prmEntity._userid);

            //-------以上是否可用观察者模式...??--------//


            return 1;
        }

        public static bool Delete(int prmGiftProductID)
        {
            return Delete(new clsGiftProduct(prmGiftProductID));
        }

        public static bool Delete(clsGiftProduct prmGift)
        {
            int cnt = prmGift._giftproductpics.Count;
            for (int i = 0; i < cnt; i++)
            {
                if (prmGift._giftproductpics[i] != "")
                {
                    clsGiftProduct.DelFile(prmGift._giftproductpics[i]);
                }
            }
            DBase dbo = new DBase();
            dbo.ExecSql("DELETE FROM [tbl_GiftProduct] WHERE GiftProductID=" + prmGift._giftproductid.ToString());
            dbo.CloseConn();
            dbo = null;

            //对应产品分类统计数减一            
            new clsGiftProductClass(prmGift._giftproductclassid).DecGiftProduct();

            //对应用户所有产品数减一
            clsUserCompanyInfo.DecGiftProduct(prmGift._userid);

            //-------以上是否可用观察者模式...??--------//

            return true;
        }

        public static bool Update(clsGiftProduct prmEntity)
        {
            DataTableManage SaveDTM = new DataTableManage();
            string sql;
            sql = "UPDATE [tbl_GiftProduct] SET " +
                "[UserID] = @UserID, " +
                "[GiftProductName] = @GiftProductName, " +
                "[GiftProductClassID] = @GiftProductClassID, " +
                "[GiftProductTypeID] = @GiftProductTypeID, " +
                "[GiftProductInfo] = @GiftProductInfo, " +
                "[GiftProductPriceSpanID] = @GiftProductPriceSpanID, " +
                "[GiftProductUnit] = @GiftProductUnit, " +
                "[GiftProductMinOrder] = @GiftProductMinOrder, " +
                "[GiftProductOuterTime] = @GiftProductOuterTime, " +
                "[GiftProductAddTime] = @GiftProductAddTime, " +
                    "[GiftProductMaterial] = @GiftProductMaterial, " +
                    "[GiftProductPacking] = @GiftProductPacking, " +
                    "[GiftProductSize] = @GiftProductSize, " +
                    "[PackingNum] = @PackingNum, " +
                    "[GiftProductFavorites] = @GiftProductFavorites, " +
                "[GiftProductPic1] = @GiftProductPic1, " +
                "[GiftProductPic2] = @GiftProductPic2, " +
                "[GiftProductPic3] = @GiftProductPic3, " +
                "[GiftProductPic4] = @GiftProductPic4, " +
                "[GiftProductPic5] = @GiftProductPic5, " +
                "[GiftProductHits] = @GiftProductHits " +
            " WHERE GiftProductID=@GiftProductID";
            SaveDTM.Sql = sql;
            SaveDTM.AddNewParam("@UserID", prmEntity._userid);
            SaveDTM.AddNewParam("@GiftProductName", prmEntity._giftproductname);
            SaveDTM.AddNewParam("@GiftProductClassID", prmEntity._giftproductclassid);
            SaveDTM.AddNewParam("@GiftProductTypeID", prmEntity._giftproducttypeid);
            SaveDTM.AddNewParam("@GiftProductInfo", prmEntity._giftproductinfo);
            SaveDTM.AddNewParam("@GiftProductPriceSpanID", prmEntity._giftproductpricespanid);
            SaveDTM.AddNewParam("@GiftProductUnit", prmEntity._giftproductunit);
            SaveDTM.AddNewParam("@GiftProductMinOrder", prmEntity._giftproductminorder);
            SaveDTM.AddNewParam("@GiftProductOuterTime", prmEntity._giftproductoutertime);
            SaveDTM.AddNewParam("@GiftProductAddTime", prmEntity._giftproductaddtime);
            SaveDTM.AddNewParam("@GiftProductMaterial", prmEntity._giftproductmaterial);
            SaveDTM.AddNewParam("@GiftProductPacking", prmEntity._giftproductpacking);
            SaveDTM.AddNewParam("@GiftProductSize", prmEntity._giftproductsize);
            SaveDTM.AddNewParam("@PackingNum", prmEntity._packingnum);
            SaveDTM.AddNewParam("@GiftProductFavorites", prmEntity._giftproductfavorites);
            for (int i = 1; i <= 5; i++)
            {
                SaveDTM.AddNewParam("@GiftProductPic" + i, prmEntity._giftproductpics[i - 1]);
            }
            SaveDTM.AddNewParam("@GiftProductHits", prmEntity._giftproducthits);
            SaveDTM.AddNewParam("@GiftProductID", prmEntity._giftproductid);
            SaveDTM.Update();
            return true;
        }

        #endregion

        public static List<clsGiftProduct> GetCollection()
        {
            List<clsGiftProduct> ls = new List<clsGiftProduct>();
            DBase dbo = new DBase();
            string sql = "SELECT * FROM [tbl_GiftProduct] ORDER BY GiftProductID DESC";
            System.Data.IDataReader dr = dbo.ExecuteReader(sql);
            while (dr.Read())
            {
                ls.Add(DrToClass(dr));
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            return ls;
        }

        public static List<clsGiftProduct> GetCollection(int prmUserID)
        {
            return GetCollection(0, prmUserID);
        }

        public static List<clsGiftProduct> GetCollection(int prmRow, int prmUserID)
        {
            List<clsGiftProduct> ls = new List<clsGiftProduct>();
            DBase dbo = new DBase();
            string sql = string.Format("SELECT TOP {0} * FROM [tbl_GiftProduct] WHERE UserID={1} ORDER BY GiftProductID DESC", prmRow, prmUserID);
            System.Data.IDataReader dr = dbo.ExecuteReader(sql);
            while (dr.Read())
            {
                ls.Add(DrToClass(dr));
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            return ls;
        }

        public static List<clsGiftProduct> GetCollection(int ClassID, int OrderType, string SearchKey, int PriceSpanId, int AreaId)
        {
            List<clsGiftProduct> ls = new List<clsGiftProduct>();
            DBase dbo = new DBase();
            string sql = "SELECT * FROM ";
            if (ClassID > 0)
            {
                clsGiftProductClass cls = new clsGiftProductClass(ClassID);
                if (cls.GiftProductClassID > 0)
                {
                    sql += " [vi_GiftProdcut] WHERE GiftProductClassCode LIKE '" + cls.GiftProductClassCode + "%'";
                }
                else
                {
                    sql += " [tbl_GiftProduct] WHERE 1=1 ";
                }
            }
            else
            {
                sql += " [tbl_GiftProduct] WHERE 1=1 ";
            }


            if (PriceSpanId > 0)
            {
                sql += " AND GiftProductPriceSpanID =" + PriceSpanId;
            }

            if (AreaId > 0)
            {
                //sql += " AND AreaId =" + AreaId;
            }

            if (SearchKey.Trim().Length > 0)
            {
                sql += " AND GiftProductName LIKE '%" + SearchKey.Trim() + "%'";
            }

            if (OrderType == 0)
            {
                sql += " ORDER BY GiftProductID DESC";
            }
            else
            {
                sql += " ORDER BY GiftProductHits DESC";
            }
            System.Data.IDataReader dr = dbo.ExecuteReader(sql);
            while (dr.Read())
            {
                ls.Add(DrToClass(dr));
            }
            dr.Dispose();
            dr = null;
            dbo.CloseConn();
            dbo = null;
            return ls;
        }

        public static void DelFile(string prmPathFile)
        {
            string strRootPath = System.Web.HttpContext.Current.Server.MapPath("~/");
            strRootPath += "/" + prmPathFile;
            if (System.IO.File.Exists(strRootPath))
                System.IO.File.Delete(strRootPath);
        }

        #region 字段属性

        private int _giftproductid = 0;
        /// <summary>
        /// 礼品ID-自增
        /// </summary>
        public int GiftProductID
        {
            get { return _giftproductid; }
            set { _giftproductid = value; }
        }

        private int _userid = 0;
        /// <summary>
        /// 所属用户
        /// </summary>
        public int UserID
        {
            get { return _userid; }
            set { _userid = value; }
        }

        private string _giftproductname = "";
        /// <summary>
        /// 礼品/服务名称
        /// </summary>
        public string GiftProductName
        {
            get { return _giftproductname; }
            set { _giftproductname = value; }
        }

        private int _giftproductclassid = 0;
        /// <summary>
        /// 自定义产品分类
        /// </summary>
        public int GiftProductClassID
        {
            get { return _giftproductclassid; }
            set { _giftproductclassid = value; }
        }

        private int _giftproducttypeid = 0;
        /// <summary>
        /// 信息类别（管理）
        /// </summary>
        public int GiftProductTypeID
        {
            get { return _giftproducttypeid; }
            set { _giftproducttypeid = value; }
        }

        private string _giftproductinfo = "";
        /// <summary>
        /// 详细说明
        /// </summary>
        public string GiftProductInfo
        {
            get { return _giftproductinfo; }
            set { _giftproductinfo = value; }
        }

        private int _giftproductpricespanid = 0;
        /// <summary>
        /// 所属价格区间ID
        /// </summary>
        public int GiftProductPriceSpanID
        {
            get { return _giftproductpricespanid; }
            set { _giftproductpricespanid = value; }
        }
        public string GiftProductPriceSpanName
        {
            get 
            {
                string ret = "";
                clsGiftProductPriceSpan ps = new clsGiftProductPriceSpan(this.GiftProductPriceSpanID);
                if (ps.GiftProductPriceSpanID > 0)
                    ret = ps.GiftProductPriceSpanName;
                return ret; 
            }
        }
        public string UserCompanyName
        {
            get
            {
                string ret = "";
                clsUserCompanyInfo comp = new clsUserCompanyInfo(this.UserID);
                if (comp.UserID > 0)
                    ret = comp.CompanyName;
                return ret;
            }
        }

        private string _giftproductunit = "";
        /// <summary>
        /// 计量单位
        /// </summary>
        public string GiftProductUnit
        {
            get { return _giftproductunit; }
            set { _giftproductunit = value; }
        }

        private int _giftproductminorder = 0;
        /// <summary>
        /// 最小起订量
        /// </summary>
        public int GiftProductMinOrder
        {
            get { return _giftproductminorder; }
            set { _giftproductminorder = value; }
        }

        private int _giftproductoutertime = 0;
        /// <summary>
        /// 出货时间(天数）
        /// </summary>
        public int GiftProductOuterTime
        {
            get { return _giftproductoutertime; }
            set { _giftproductoutertime = value; }
        }

        private DateTime _giftproductaddtime = new DateTime(1900, 1, 1);
        /// <summary>
        /// 礼品添加时间
        /// </summary>
        public DateTime GiftProductAddTime
        {
            get { return _giftproductaddtime; }
            set { _giftproductaddtime = value; }
        }
        private string _giftproductmaterial = "";
        /// <summary>
        /// 商品材质
        /// </summary>
        public string GiftProductMaterial
        {
            get { return _giftproductmaterial; }
            set { _giftproductmaterial = value; }
        }

        private string _giftproductpacking = "";
        /// <summary>
        /// 商品包装
        /// </summary>
        public string GiftProductPacking
        {
            get { return _giftproductpacking; }
            set { _giftproductpacking = value; }
        }

        private string _giftproductsize = "";
        /// <summary>
        /// 商品尺寸
        /// </summary>
        public string GiftProductSize
        {
            get { return _giftproductsize; }
            set { _giftproductsize = value; }
        }

        private int _packingnum = 0;
        /// <summary>
        /// 装箱数量
        /// </summary>
        public int PackingNum
        {
            get { return _packingnum; }
            set { _packingnum = value; }
        }

        private int _giftproductfavorites = 0;
        /// <summary>
        /// 被收藏数
        /// </summary>
        public int GiftProductFavorites
        {
            get { return _giftproductfavorites; }
            set { _giftproductfavorites = value; }
        }


        private int _giftproducthits = 0;
        /// <summary>
        /// 被点击数
        /// </summary>
        public int GiftProductHits
        {
            get { return _giftproducthits; }
            set { _giftproducthits = value; }
        }

        private List<string> _giftproductpics = new List<string>();
        /// <summary>
        /// 产品图
        /// </summary>
        public List<string> GiftProductPics
        {
            get { return _giftproductpics; }
            set { _giftproductpics = value; }
        }

        /// <summary>
        /// 产品图
        /// </summary>
        public string GiftProductPic
        {
            get
            {
                string ret = "";
                int cnt = this._giftproductpics.Count;
                if (cnt > 0)
                {
                    for (int i = 0; i < cnt; i++)
                    {
                        if (this._giftproductpics[i] != "")
                        {
                            ret = this._giftproductpics[i];
                            break;
                        }
                    }
                }
                return ret;
            }
        }

        #endregion



        public void GiftProductHit()
        {
            string sql = string.Format("UPDATE [tbl_GiftProduct] SET GiftProductHits=GiftProductHits+1 WHERE (GiftProductID = {0})", this._giftproductid);
            DBase dbo = new DBase();
            dbo.ExecSql(sql);
            dbo.CloseConn();
            dbo = null;
        }
    }
}
